import {createStore} from 'vuex';
import createPersistedState from 'vuex-persistedstate';
import router from '../router';
import Cookie from "js-cookie"
//vuex不能做数据持久化 就要结合本地存储
export default createStore({
    state:{
        isCollapse:true,
        currentMenu:null,
        tabsList:[
            {
                path:'/',
                name:'home',
                label:'首页',
                icon:'home'
            }
        ],
        menu:[],
        routes:[],
        token:'',
    },
    mutations:{
        updateIsCollspse(state,payload){
            state.isCollapse=!state.isCollapse
        },
        selectMenu(state,val){
            //判断
            // val.name==='home'?(state.currentMenu=null):(state.currentMenu=val)
            if(val.name=='home'){
                 state.currentMenu=null
            }else{
                state.currentMenu=val
                let result=state.tabsList.findIndex(item=>item.name===val.name)
                result==-1?state.tabsList.push(val):''
                
            }
        },
        closeTab(state,val){
           let res= state.tabsList.findIndex(item=>item.name===val.name)
           state.tabsList.splice(res,1)
        },
        setMenu(state,[val,router]){
            state.menu=val
            // localStorage.setItem('menu',JSON.stringify(val))
            console.log(val,router,'val')

            const menuArray=val
            const renderRouter=(menu)=>{
             return   menu.forEach(item=>{
                    if(item.children){
                       return renderRouter(item.children)
                        // item.children=item.children.map((item)=>{
                        //     let url=`../views/${item.url}.vue`
                        //     item.component=()=>import(url)
                        //     menuArray.push(...item.children)
                        // })
                     }else{
                        let url=`../views/${item.url}.vue`
                        item.component=()=>import(url)
                        console.log(item,'item')
                        item.path=item.redirect?'/'+item.redirect:item.path
                        return item
                        // menuArray.push(...item)
                    }
                })
            }
            const result=renderRouter(menuArray)
            console.log(menuArray,'result')
            // state.routes=menuArray
            // state.menu.forEach(item=>{
            //     if(item.children){
            //         item.children=item.children.map((item)=>{
            //             let url=`../views/${item.url}.vue`
            //             item.component=()=>import(url)
            //             menuArray.push({...item.children})
            //         })
            //      }else{
            //         let url=`../views/${item.url}.vue`
            //         item.component=()=>import(url)
            //         menuArray.push({...item})
            //     }
            // })
            menuArray.forEach(item=>{
                router.addRoute('home1',item)
            })
        },
        addMenu(state,router){
            if(!localStorage.getItem('menu')){
                return
            }
            const menu=JSON.parse(localStorage.getItem('menu'))
            state.menu=menu.menu

            const menuArray=menu
            const renderRouter=(menu)=>{
             return   menu.forEach(item=>{
                    if(item.children){
                       return renderRouter(item.children)
                        // item.children=item.children.map((item)=>{
                        //     let url=`../views/${item.url}.vue`
                        //     item.component=()=>import(url)
                        //     menuArray.push(...item.children)
                        // })
                     }else{
                        let url=`../views/${item.url}.vue`
                        item.component=()=>import(url)
                        console.log(item,'item')
                        item.path=item.redirect?'/'+item.redirect:item.path
                        return item
                        // menuArray.push(...item)
                    }
                })
            }
            const result=renderRouter(menuArray)
            console.log(menuArray,'result')
            // state.routes=menuArray
            // state.menu.forEach(item=>{
            //     if(item.children){
            //         item.children=item.children.map((item)=>{
            //             let url=`../views/${item.url}.vue`
            //             item.component=()=>import(url)
            //             menuArray.push({...item.children})
            //         })
            //      }else{
            //         let url=`../views/${item.url}.vue`
            //         item.component=()=>import(url)
            //         menuArray.push({...item})
            //     }
            // })
            menuArray.forEach(item=>{
                router.addRoute('home1',item)
            })
        },
        clearMenu(state){
            console.log('清楚')
            state.menu=[]
            localStorage.removeItem('menu')
        },
        setToken(state,val){
            state.token=val
            // Cookie.set('token',val)
            document.cookie=`token=${val}`
            console.log(document.cookie,'ciikie')
        },
        clearToken(state,val){
            state.token=''
            Cookie.remove('token')
        },
        getToken(state){
            state.token=state.token||Cookie.get('token')
        }
    },
    plugins: [
 
        createPersistedState({
 
            key: 'menu', // 存储是的名字
 
            // 也可以是sessionstorage
 
            storage: window.localStorage
 
        })
    ]
})